package com.redis.template;


import com.redis.enums.RedisModeEnum;

import java.util.concurrent.TimeUnit;

/**
 * @author jinYuLinLang
 * @version 1.0.0
 * @ClassName MyRedisTemplate.java
 * @Description <p></p>
 * @createTime 2024-08-29 19:46:00
 */
public abstract class MyRedisTemplate {


    /**
     * @param hosts 127.0.0.1:6379,127.0.0.2:6379, 127.0.0.3:6379,
     * @return
     */
    public static MyRedisTemplate defaultTemplate(String hosts) {
        return defaultTemplate(hosts, null, null);
    }


    /**
     * 默认模板
     *
     * @param hosts
     * @param username
     * @param password
     * @return
     */
    public static MyRedisTemplate defaultTemplate(String hosts, String username, String password) {
        int length = hosts.split(",").length;
        if (length == 1) {
            return createTemplate(hosts, username, password, RedisModeEnum.SINGLE);
        }
        if (length > 1) {
            return createTemplate(hosts, username, password, RedisModeEnum.CLUSTER);
        }
        throw new RuntimeException("illegal redis mode,only support CLUSTER or SINGLE");
    }

    /**
     * 根据redis 的部署模式创建不同的方式
     *
     * @param hosts
     * @param username
     * @param password
     * @param redisModeEnum CLUSTER：集群版， SINGLE：单机版
     * @return
     */
    public static MyRedisTemplate createTemplate(String hosts,
                                                 String username,
                                                 String password,
                                                 RedisModeEnum redisModeEnum) {
        if (redisModeEnum.equals(RedisModeEnum.SINGLE)) {

            return new JedisPoolTemplate(hosts, username, password);
        }

        if (redisModeEnum.equals(RedisModeEnum.CLUSTER)) {

            return new JedisCLusterTemplate(hosts, username, password);
        }
        throw new RuntimeException("illegal redis mode,only support CLUSTER or SINGLE");
    }


    public abstract String get(String key);

    public abstract String set(String key, String value);

    public abstract String set(String key, String value, TimeUnit timeUnit, long timeout);

    public abstract Long del(String key);

    public abstract Long append(String key, String value);

    public abstract Boolean exists(String key);

    public abstract Long expire(String key, long seconds);

    public abstract Long ttl(String key);

    public abstract void close();

}
